package com.slicktechnologies.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.shared.GwtIncompatible;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.annotation.OnSave;
import com.itextpdf.text.log.SysoCounter;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.slicktechnologies.client.config.configurations.ConfigTypes;
import com.slicktechnologies.server.ContractServiceImplementor;
import com.slicktechnologies.server.QuotationServiceImplentor;
import com.slicktechnologies.shared.common.businessprocesslayer.ConcreteBusinessProcess;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.helperlayer.Config;
import com.slicktechnologies.shared.common.productlayer.ServiceProduct;

import static com.googlecode.objectify.ObjectifyService.ofy;

/**
 * Represents the Quotation of AMC only.
 */
@Entity

public class Quotation extends Sales implements Serializable
{
	
	public final static String  QUOTATIONSUCESSFUL="Successful";
	public final static String  QUOTATIONUNSUCESSFUL="UnSuccessful";
	
	/**************************************************Entity Attributes**********************************************/
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -4383102276376664406L;

	/** The grand total of all sales line items */
	protected double grandTotal;

	/** The flat discount on grand total*/
	protected double flatDiscount;

	/** The per discount on grand total */
	protected double perDiscount;

	/** The total Amount to be paid */
	protected double netpayable;

	/** The valid until. Expiry date of the quotation*/
	protected Date validUntill;

	/**Priority Of Quotation*/
	@Index
	protected String priority;
	
	/**************************************************Relational Part************************************************************/
    protected Key<Config>keyQuotationPriority;
    protected boolean statusChanged=false;
	private Date startDate;
	private Date endDate;
	
	
	
	/**************************************************Default Ctor**********************************************/
	/**
	 * Instantiates a new quotation.
	 */
	public Quotation()
	{
		super();
		priority="";
		this.status=CREATED;
		statusChanged=false;
	}

	/**************************************************Getter/Setter**********************************************/
	/**
	 * Gets the net payable.
	 *
	 * @return the net payable
	 */
	


	/**
	 * Sets the net payable.
	 *
	 * @param netpayable the new net payable
	 */
	

	/* (non-Javadoc)
	 * @see com.simplesoftwares.shared.businessprocesslayer.ConcreteBuisnessProcess#compareTo(com.simplesoftwares.shared.helperlayer.SuperModel)
	 */
	@Override
	public int compareTo(SuperModel o) {
		Quotation quot=(Quotation) o;
		return quot.creationDate.compareTo(creationDate);
	}



	/**
	 * Sets the grand total.
	 *
	 * @param grandTotal the new grand total
	 */
	public void setGrandTotal(Double grandTotal) {
		if(grandTotal!=null)
		   this.grandTotal = grandTotal;
	}

	/**
	 * Gets the grand total.
	 *
	 * @return the grand total
	 */
	public double getGrandTotal() {
		return grandTotal;
	}



	/**
	 * Gets the flat discount.
	 *
	 * @return the flat discount
	 */
	public double getFlatDiscount() {
		return flatDiscount;
	}


	/**
	 * Sets the flat discount.
	 *
	 * @param flatDiscount the new flat discount
	 */
	public void setFlatDiscount(Double flatDiscount) {
		if(flatDiscount!=null)
		   this.flatDiscount = flatDiscount;
	}


	/**
	 * Gets the per discount.
	 *
	 * @return the per discount
	 */
	public double getPerDiscount() {
		return perDiscount;
	}


	/**
	 * Sets the per discount.
	 *
	 * @param perDiscount the new per discount
	 */
	public void setPerDiscount(Double perDiscount) {
		if(perDiscount!=null)
			this.perDiscount = perDiscount;
	}


	/**
	 * Gets the netpayable.
	 *
	 * @return the netpayable
	 */
	public double getNetpayable() {
		return netpayable;
	}


	/**
	 * Sets the netpayable.
	 *
	 * @param netpayable the new netpayable
	 */
	public void setNetpayable(Double netpayable) {
		if(netpayable!=null)
			this.netpayable = netpayable;
	}


	/**
	 * Gets the validuntil.
	 *
	 * @return the validuntil
	 */
	public Date getValidUntill() {
		return validUntill;
	}


	/**
	 * Sets the validuntil.
	 *
	 * @param validuntil the new validuntil
	 */
	public void setValidUntill(Date validuntil) {
		this.validUntill = validuntil;
	}


	/* (non-Javadoc)
	 * @see com.simplesoftwares.shared.businessprocesslayer.ConcreteBuisnessProcess#isDuplicate(com.simplesoftwares.shared.helperlayer.SuperModel)
	 */
	@Override
	public boolean isDuplicate(SuperModel m) {
		// TODO Auto-generated method stub
		return false;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) 
	{
		if(priority!=null)
			this.priority = priority.trim();
	}
	
	
	
	@Override
	public String getStatus() {
		return super.getStatus();
	}
	
	

	
	/**************************************************Relation Managment Part**********************************************/
	@OnSave
	@GwtIncompatible
	public void manageQuotationRelation()
	{
			
				
	}
	
	@OnLoad
	@GwtIncompatible
	public void OnLoad()
	{	

	}
	/*********************************************************************************************************************/


    
    	
    		

	public static List<String> getStatusList()
	{
		ArrayList<String> statuslist=new ArrayList<String>();
		statuslist.add(CREATED);
		statuslist.add(QUOTATIONSUCESSFUL);
		statuslist.add(QUOTATIONUNSUCESSFUL);
		statuslist.add(APPROVED);
		statuslist.add(Contract.REJECTED);
		statuslist.add(Contract.REQUESTED);
		
		return statuslist;
	}
	
	
	
	public Date getStartDate()
	{
		return startDate;
	}
	
	
	public Date getEndDate()
	{
		return endDate;
	}

/**********************************************************************************************************************/	
	
	
	
	
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	

	@GwtIncompatible
	@Override
	public void reactOnApproval() {
		QuotationServiceImplentor implementor=new QuotationServiceImplentor();
		implementor.changeStatus(this);
	}
	
	
	
	
	
}
